/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import Entity.User;
import Facade.exceptions.NonexistentEntityException;
import VO.UserVO;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import service.UserService;
import java.util.ArrayList;


/**
 *
 * @author Edwin
 */
public class UserFacade {
    
    private UserService serviceUser;
    private UserVO userVO;
    private User userEntity;
    

    public UserFacade() {
        serviceUser = new UserService();
        userVO = new UserVO();
        userEntity = new User();
    }       

   public UserVO loginUser(String name, String password){
        UserVO adminVO_aux= new UserVO();
        adminVO_aux.setLogin(name);
        adminVO_aux.setPassword(password);
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            userVO=serviceUser.correctLoginUser(adminVO_aux,em);
            em.getTransaction().commit();
            return userVO;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    
    public List<UserVO> getUserList(){
        List<UserVO> userVO_aux= new ArrayList<UserVO>();
        List<User> user = new ArrayList<User>();
        UserVO userVO = new UserVO();
       
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            user=serviceUser.getUserList(em);
            
            for(int i=0;i<user.size();i++){
                
                userVO=userEntity.toVO(user.get(i));
                userVO_aux.add(userVO);
            }
            
            
            em.getTransaction().commit();
            return userVO_aux;
        } finally {
            if (em != null) {
                em.close();
            }
        
        }
    }
    
    public UserFacade(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        emf = Persistence.createEntityManagerFactory("ProyectoFinalPU2");
        return emf.createEntityManager();
    }

    public void createAdmin(UserVO administratorVO) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            serviceUser.persistUser(administratorVO, em);
            
            //em.persist(administratorVO);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    public void create(UserVO user) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            serviceUser.persistUser(user, em);
            //em.persist(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    
    public void eraseUser(UserVO userVO) {
        User user=new User();
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            if(userVO!=null){
            user=serviceUser.convertToEntityUser(userVO); 
            
            user = em.getReference(User.class, user.getId());
            user.getId();
            }
            
            em.remove(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
    
    
    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            User user;
            try {
                user = em.getReference(User.class, id);
                user.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The user with id " + id + " no longer exists.", enfe);
            }
            em.remove(user);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    
    
}
